package org.iabako.client.ui.form.business.sale.make;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.DefaultSuggestHandler;
import org.iabako.client.tools.DefaultSuggestSelectionHandler;
import org.iabako.client.tools.Messages;
import org.iabako.client.ui.layout.field.CheckboxBoolean;
import org.iabako.client.ui.layout.field.InputTextSuggest;
import org.iabako.client.ui.layout.field.LabelText;
import org.iabako.client.ui.layout.widget.validation.ValidationMessage;
import org.iabako.shared.entity.business.Client;
import org.iabako.shared.entity.business.Sale;
import org.iabako.shared.entity.enumeration.SaleStep;
import org.iabako.shared.iservice.business.ClientServiceAsync;

/**
 * Created by jose on 21/10/14.
 */
public class SaleMakeClientChoice extends SaleMakeSubForm {

    interface MyUiBinder extends UiBinder<HTMLPanel, SaleMakeClientChoice> {
    }

    protected MyUiBinder binderForm = GWT.create(MyUiBinder.class);

    @UiField
    InputTextSuggest clientInput;
    @UiField
    CheckboxBoolean saleWithNoClient;
    @UiField
    LabelText noClientPaymentNote;
    @UiField
    LabelText addNewClientAction;
    @UiField
    HTMLPanel clientListOfSaleEnMassePanel;
    @UiField
    LabelText clientListOfSaleEnMasse;

    public DefaultSuggestSelectionHandler clientSuggestSelectionHandler;

    public SaleMakeClientChoice() {
        initWidget(binderForm.createAndBindUi(this));

        setAddNewClientHandler();
        setSaleWithNoClientHandler();
        setSuggestionFieldsHandlers();

        setDefaultHandlers();
    }

    @Override
    public void restoreDraft() {
        //For draft handling
        if (saleMakeForm.entity != null) {
            Client client = ((Sale) saleMakeForm.entity).getClient();
            saleMakeForm.client = client;
            clientInput.setValue(client != null ? client.getName() : null);
            clientSuggestSelectionHandler.setEntity(client);
            clientSuggestSelectionHandler.setHiddenSelectionCode(client != null ? client.getId() + "" : null);

            if (((Sale) saleMakeForm.entity).getSaleStep() != SaleStep.who && client == null) {
                setNoClientValues(false);
            }
        }
    }

    public void activateQuoteMode() {
        addNewClientAction.setLabel("quote_form_create_new_client");
        saleWithNoClient.setLabel("quote_no_client");
    }

    public void activateSaleEnMasse() {
        clientInput.setVisible(false);
        saleWithNoClient.setVisible(false);
        addNewClientAction.setVisible(false);
        clientListOfSaleEnMassePanel.setVisible(true);
        clientListOfSaleEnMasse.setLabel(Messages.getLabel("sales_en_masse_see_client_list_link", saleMakeForm.clientList.size() + ""));

        clientListOfSaleEnMasse.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                saleMakeForm.showDialogBoxWithClientList();
            }
        });

        saleMakeForm.saleDraftAction.setVisible(false);
    }

    private void setAddNewClientHandler() {

        addNewClientAction.getLabel().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                NavigationController.getInstance().goTo(PageEnum.ClientCreate, saleMakeForm);
            }
        });
    }


    private void setSaleWithNoClientHandler() {
        saleWithNoClient.getCheckbox().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                setNoClientValues(!saleWithNoClient.getValue());
            }
        });
    }

    private void setNoClientValues(boolean saleHasClient) {
        saleWithNoClient.setValue(!saleHasClient);
        clientInput.enableNoValueString();
        clientInput.setEnabled(saleHasClient);
        clientSuggestSelectionHandler.cleanValue();
        saleMakeForm.setSaleWithNoClient(!saleHasClient);
        if (!saleHasClient) {
            clientSuggestSelectionHandler.cleanValue();
        }
    }

    private void setSuggestionFieldsHandlers() {
        clientInput.setSuggestHandler(new DefaultSuggestHandler(ClientServiceAsync.Util.getInstance()));
        clientSuggestSelectionHandler = new DefaultSuggestSelectionHandler(ClientServiceAsync.Util.getInstance(), clientInput);
        clientInput.setSelectionHandler(clientSuggestSelectionHandler);
        clientInput.getInputField().addKeyUpHandler(clientSuggestSelectionHandler);
        clientInput.getInputField().addBlurHandler(clientSuggestSelectionHandler);
    }

    protected void validateForm() {
        if (!saleMakeForm.saleEnMasse && !saleWithNoClient.getValue() && clientSuggestSelectionHandler.getEntity() == null) {
            ValidationMessage.getFirstFromDOM().showErrorMessage(true, true, "validation_sale_make_no_client_choice_error");
            return;
        }
        saleMakeForm.client = (Client) clientSuggestSelectionHandler.getEntity();

        saleMakeForm.saleMakeProductServiceChoice.updateSaleWithClientFields();

        if (!saleMakeForm.saleEnMasse && saleMakeForm.client == null) {
            saleMakeForm.saleMakeProductServiceChoice.cleanServiceUndefinedInstallments();
        }

        saleMakeForm.goTo(SaleStep.what);
    }

    @Override
    protected void goBackAction() {
        //Nothing
    }
}
